#!/bin/bash

ARG=$1
TAG=${2-'latest'}
CONTAINER_NAME=aspect-server

imageBuild() {
  # image build
  CURRENT_DATE=$(date "+%Y-%m-%d %H:%M:%S")
  docker build \
    --build-arg INPUT_DATE="${CURRENT_DATE}" \
    --build-arg JAR_FILE=./target/supreme-aspect-server-0.0.1.jar \
    --platform linux/amd64 \
    --no-cache \
    -t majewing/supreme-aspect-server:$TAG -f ./Dockerfile .
}

imageRunner() {
  CONTAINER_DIR="${HOME}/Development/docker-data/supreme-server/aspect-server"
  docker run \
    --rm \
    --platform linux/x86_64 \
    --name $CONTAINER_NAME \
    -p 7221:7220 \
    -e ASPECT_CONTROLLER="DockerRunController" \
    -e ASPECT_SERVICE="DockerRunService" \
    -e TEMP_CONTENT="DockerRunContent" \
    -v $CONTAINER_DIR/logs:/home/logs \
    -d supreme-aspect-server:$TAG \
    --aspect-server.startCommand=docker-run-start-command --aspect-server.tempContent=CmdDockerRunContent
}

if [ -z "$ARG" ]; then
    echo "[ build | run | stop | logs | tag | push ]"
    exit 1
fi

if [ "$ARG" == "build" ]; then
  imageBuild
elif [ "$ARG" == "run" ]; then
  imageRunner
elif [ "$ARG" == "stop" ]; then
  docker stop $CONTAINER_NAME
elif [ "$ARG" == "logs" ]; then
  docker logs -f $CONTAINER_NAME
elif [ "$ARG" == "tag" ]; then
  docker tag supreme-aspect-server:$TAG majewing/supreme-aspect-server:$TAG
elif [ "$ARG" == "push" ]; then
  docker push majewing/supreme-aspect-server:$TAG
elif [ "$ARG" == "package" ]; then
  mvn -Dmaven.test.skip -U clean package
fi
